import requests
from lxml import etree
import utils


# 牛视网
class NiuShiWang(object):
    host = 'http://www.nnzhibo.com/'
    url = host + 'gangtaizhibo/index.html'

    def __init__(self):
        self.encoding = "GB2312"
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Mobile Safari/537.36"
        }
        self.session = requests.session()

    def get_data(self, url=url):
        result = self.session.get(url)
        result.encoding = self.encoding
        return result.text

    data = []

    def testurl(self, url):
        if not url.startswith('http'):
            return False
        try:
            result = self.session.get(url, timeout=1)
            return result.status_code == 200
        except:
            return False

    def parse_data(self, html_data):
        html = etree.HTML(html_data)
        result = html.xpath("//div[@class='thumbnail']/a")
        for item in result:
            zhiboyuan = {
                "url": self.host + item.xpath("./@href")[0],
                "name": item.xpath("./@title")[0]
            }
            self.data.append(zhiboyuan)
        self.next_page(html)
        return self.data

    def next_page(self, html):
        a = html.xpath("//div[@class='pagenavi']/a")[-2]
        next_url = self.host + a.xpath('./@href')[0]
        next = a.xpath("./span[@class='page']/text()")
        if next and next[0] == '下一页':
            data_html = self.get_data(next_url)
            self.parse_data(data_html)

    pass

    def get_live_source(self, source):
        result = self.session.get(source['url'])
        result.encoding = self.encoding
        html = etree.HTML(result.text)
        page_url = html.xpath("//div[@class='ad']/a/@href")
        if page_url:
            live_source_url = self.session.get(self.host + page_url[0])
            live_source_url.encoding = self.encoding
            html = etree.HTML(live_source_url.text)
            url = html.xpath('//easy-player/@video-url')
            if url and url[0]:
                live_source = {
                    'name': source['name'],
                    'url': url[0]
                }
                return live_source

    def get_live_sources(self):
        html_data = niuShiWang.get_data()
        live_sources = []
        data = niuShiWang.parse_data(html_data)
        for item in data:
            live_sources.append(niuShiWang.get_live_source(item))
        return live_sources


if __name__ == '__main__':
    niuShiWang = NiuShiWang()
    live_sources = niuShiWang.get_live_sources()
    lists = []
    for item in live_sources:
        if item:
            url = item['url']
            if url:
                temp = utils.Utils.rtmp2flv(url)
                item['url'] = temp
                if niuShiWang.testurl(temp):
                    lists.append(item)
                    print(item)
    print(lists)
    # result = requests.get("rtmp://ws.matchupstream.tuboshu.com/live/bfe61991c3bd3daab7009d922c53f59c")
    # print(result.status_code)
